package com.code.demo.ui.gallery;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.code.demo.R;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mikeafc on 15/11/26.
 */
public class GalleryPagerAdapter extends PagerAdapter {

    private Context mContext;
    private boolean isMultiScr;
    private Handler mHandler;
    private List<String> mDataList = new ArrayList<>();

    public GalleryPagerAdapter(Context context, boolean isMultiScr) {
        this.mContext = context;
        this.isMultiScr = isMultiScr;
    }

    public void setData(Handler handler, List<String> list) {
        this.mHandler = handler;
        this.mDataList = list;
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.layout_pager_adapter, container, false);
        final PhotoView photoView = view.findViewById(R.id.iv_photo);
        String image = mDataList.get(position);
        Glide.with(container.getContext().getApplicationContext())
                .load(image)
                .into(photoView);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHandler.sendEmptyMessage(0);
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        View view = (LinearLayout) object;
        container.removeView((View) object);
    }

}
